{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import Well Log Curve Data from file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, say we have a new well `CUG3`. After writing the `CUG3.well` file, and save it to `Wellinfo` folder. We initialize the survey."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'No well named cug3'\n"
     ]
    }
   ],
   "source": [
    "survey = ppp.Survey(Path(SURVEY_FOLDER))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A meassage shows \"no well named `cug3`\", it's because there is no data stored in `well_data.h5` file. (In `pyGeoPressure`, well log data is stored in hdf5 file.)\n",
    "\n",
    "But we can still get its information:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{u'CUG1': <pygeopressure.basic.well.Well at 0x11031588>,\n",
       " u'CUG3': <pygeopressure.basic.well.Well at 0x10a0a8d0>}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "survey.wells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cug3 = survey.wells['CUG3']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We provides two methods for importing well log curve data:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Read las/pseudo-las file\n",
    "\n",
    "First, we need to read data from file.\n",
    "`pyGeoPressure` provides a class `LasData` for reading las file and pseudo-las file. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "las_data = ppp.LasData(las_file=\"C:/Users/yuhao/Desktop/CUG_depth/log_curves.las\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "get las file type with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pseudo-las'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las_data.file_type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read pseudo-las file with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "las_data.read_pseudo_las()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. at runtime\n",
    "\n",
    "At runtime, well log curve data are stored in pandas dataframe. Each `Well` object has a `dataframe` attribute. To import well log curve to `Well`, users can directly set a new dataframe read by `LasData` to `Well.dataframe`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "cug3 = survey.wells['CUG3']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set the data_frame of `LasData` to `Well`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "cug3.data_frame = las_data.data_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'Shale_Volume',\n",
       " u'Density',\n",
       " u'Density_filter20_sm1500',\n",
       " u'Velocity',\n",
       " u'Velocity_filter20_sm1500',\n",
       " u'Overburden_Pressure',\n",
       " u'Porosity']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cug3.logs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or part of the read dataframe:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cug3.data_frame = las_data.data_frame[['Depth(m)', 'Shale_Volume(Fraction)', 'Density(G/C3)']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'Shale_Volume', u'Density']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cug3.logs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**After importing logs, users should call `save_well_logs()` to save them to storage file.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. edit `.hd5` file\n",
    "\n",
    "In `pyGeoPressure`, well log curve data is stored in hdf5 files (I call it storage file) on disk. To import well log curves, users can directly manage hdf5 file.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`pyGeoPressure` provides class `WellStorage` to manage hdf5 files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "storage = ppp.WellStorage(hdf5_file='C:/Users/yuhao/Desktop/CUG_depth/Wellinfo/well_data.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "storage.add_well(well_name='cug3', well_data_frame=las_data.data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['cug1', 'cug2', 'cug3']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "storage.wells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we reinitialize the survey, we will see that the data has been imported into Well `CUG3`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "survey = ppp.Survey(Path(SURVEY_FOLDER))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "cug3 = survey.wells['CUG3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'Shale_Volume',\n",
       " u'Density',\n",
       " u'Density_filter20_sm1500',\n",
       " u'Velocity',\n",
       " u'Velocity_filter20_sm1500',\n",
       " u'Overburden_Pressure',\n",
       " u'Porosity']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cug3.logs"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
